package com.zu.commu.auto1_0.protocol;

import com.zu.commu.auto1_0.DataType1_0;
import com.zu.commu.auto1_0.protocol.call.ReportDataProtocol;
import com.zu.commu.auto1_0.protocol.call.ReportDataProtocol2018;
import com.zu.commu.auto1_0.protocol.call20Date.Call20DateDataProtocol;
import com.zu.commu.auto1_0.protocol.call24Hour.Call24DataProtocol;
import com.zu.commu.auto1_0.protocol.charge.ChargeDataProtocol;
import com.zu.commu.auto1_0.protocol.charge.ReadChargeDataProtocol;
import com.zu.commu.auto1_0.protocol.chargeTimes.SetChargeTimesDataProtocol;
import com.zu.commu.auto1_0.protocol.clean.SetCleanDataProtocol;
import com.zu.commu.auto1_0.protocol.clock.ReadClockDataProtocol;
import com.zu.commu.auto1_0.protocol.clock.SetClockDataProtocol;
import com.zu.commu.auto1_0.protocol.initValue.SetInitValueDataProtocol;
import com.zu.commu.auto1_0.protocol.pump.SetPumpDataProtocol;
import com.zu.commu.auto1_0.protocol.reportTime.SetReportTimeAnswerProtocol;
import com.zu.commu.auto1_0.protocol.waterLevel.WaterLevelDataProtocol;
import com.zu.commu.auto1_0.protocol.waterLineWarning.WaterLineWarningDataProtocol;
import com.zu.commu.auto1_0.protocol.waterSiteWarning.WaterSiteWarningDataProtocol;
import com.zu.commu.exception.CommuException;
import com.zu.commu.interf.Data;

public class DataProtocol {
	private boolean chargeSucc = true;
	private int chargeTimes = 1;

	public boolean getChargeSucc() {
		return this.chargeSucc;
	}

	public int getChargeTimes() {
		return this.chargeTimes;
	}

	public DataType1_0 decideDataType(byte[] b) throws Exception {
		int type = -1000;
		try {
			type = b[11];
		} catch (Exception e) {
			throw new CommuException().setMessage("判断数据类型出错！", null);
		}
		if (type < 0) {
			type += 256;
		}
		DataType1_0 dt = null;
		if (type == DataType1_0.REPORTDATA.toInt())
			dt = DataType1_0.REPORTDATA;
		else if (type == DataType1_0.SETCLOCK.toInt())
			dt = DataType1_0.SETCLOCK;
		else if (type == DataType1_0.READCLOCK.toInt())
			dt = DataType1_0.READCLOCK;
		else if (type == DataType1_0.PUMP.toInt())
			dt = DataType1_0.PUMP;
		else if (type == DataType1_0.INITVALUE.toInt())
			dt = DataType1_0.INITVALUE;
		else if (type == DataType1_0.CHARGE.toInt())
			dt = DataType1_0.CHARGE;
		else if (type == DataType1_0.CHARGETIMES.toInt())
			dt = DataType1_0.CHARGETIMES;
		else if (type == DataType1_0.READCHARGE.toInt())
			dt = DataType1_0.READCHARGE;
		else if (type == DataType1_0.REPORT24DATA.toInt())
			dt = DataType1_0.REPORT24DATA;
		else if (type == DataType1_0.REPORT20DATEDATA.toInt())
			dt = DataType1_0.REPORT20DATEDATA;
		else if (type == DataType1_0.CLEANALL.toInt())
			dt = DataType1_0.CLEANALL;
		else if (type == DataType1_0.WATERWARNING.toInt())
			dt = DataType1_0.WATERWARNING;
		else if (type == DataType1_0.WATERSITEWARNING.toInt())
			dt = DataType1_0.WATERSITEWARNING;
		else if (type == DataType1_0.WATERLEVEL.toInt())
			dt = DataType1_0.WATERLEVEL;
		else if (type == DataType1_0.REPORTTIME.toInt())
			dt = DataType1_0.REPORTTIME;

		if (dt == null) {
			throw new CommuException().setMessage("没有判断出数据类型！", null);
		}
		return dt;
	}

	public Data[] analyseReportData(byte[] b) throws Exception {
		Data[] d = new Data[1];
		d[0] = new ReportDataProtocol().analyseReportData(b);
		return d;
	}

	public Data[] analyseReportData2018(byte[] b) throws Exception {
		Data[] d = new Data[1];
		d[0] = new ReportDataProtocol2018().analyseReportData(b);
		return d;
	}

	public Data[] analyseSetClockData(byte[] b)// 2018新协议占用
			throws Exception {
		Data[] d = new Data[1];
		d[0] = new SetClockDataProtocol().analyseReportData(b);
		return d;
	}

	public Data[] analyseReadClockData(byte[] b) throws Exception {
		Data[] d = new Data[1];
		d[0] = new ReadClockDataProtocol().analyseReportData(b);
		return d;
	}

	public Data[] analyseSetPumpData(byte[] b) throws Exception {
		Data[] d = new Data[1];
		d[0] = new SetPumpDataProtocol().analyseReportData(b);
		return d;
	}

	public Data[] analyseSetInitValueData(byte[] b) throws Exception {
		Data[] d = new Data[1];
		d[0] = new SetInitValueDataProtocol().analyseReportData(b);
		return d;
	}

	public Data[] analyseChargeData(byte[] b) throws Exception {
		Data[] d = new Data[1];
		d[0] = new ChargeDataProtocol().analyseReportData(b);
		if (d[0].getOrderSucc().equals("1")) {
			this.chargeSucc = false;
			this.chargeTimes = d[0].getChargeTimes().intValue();
		} else {
			this.chargeSucc = true;
		}
		return d;
	}

	public Data[] analyseSetChargeTimesData(byte[] b) throws Exception {
		Data[] d = new Data[1];
		d[0] = new SetChargeTimesDataProtocol().analyseReportData(b);
		return d;
	}

	public Data[] analyseReadChargeData(byte[] b) throws Exception {
		Data[] d = new Data[1];
		d[0] = new ReadChargeDataProtocol().analyseReportData(b);
		return d;
	}

	public Data[] analyseCall24Data(byte[] b) throws Exception {
		Data[] d = (Data[]) null;
		d = new Call24DataProtocol().analyseReportData(b);
		return d;
	}

	public Data[] analyseCall20DateData(byte[] b) throws Exception {
		Data[] d = (Data[]) null;
		d = new Call20DateDataProtocol().analyseReportData(b);
		return d;
	}

	public Data[] analyseCleanData(byte[] b) throws Exception {
		Data[] d = new Data[1];
		d[0] = new SetCleanDataProtocol().analyseReportData(b);
		return d;
	}

	public Data[] analyseWaterLineWarningData(byte[] b) throws Exception {
		Data[] d = new Data[1];
		d[0] = new WaterLineWarningDataProtocol().analyseReportData(b);
		return d;
	}

	public Data[] analyseWaterSiteWarningData(byte[] b) throws Exception {
		Data[] d = new Data[1];
		d[0] = new WaterSiteWarningDataProtocol().analyseReportData(b);
		return d;
	}

	public Data[] analyseWaterLevelData(byte[] b) throws Exception {
		Data[] d = new Data[1];
		d[0] = new WaterLevelDataProtocol().analyseReportData(b);
		return d;
	}

	public Data[] analyseReportTimeData(byte[] b) throws Exception {
		Data[] d = new Data[1];
		d[0] = new SetReportTimeAnswerProtocol().analyseReportData(b);
		return d;
	}
}